<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * goods.ctrl
 * 商品详情控制器
 */

defined('IN_IA') or exit('Access Denied');
global $_W,$_GPC;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'judgment_list';
$gid = $_GPC['gid'];
$orderno='HD20170503980646845913';
$totalnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' ");
$totaliszhuijianum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and iszhuijia=1 ");
$totalishaoyongnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and ishaoyong=1");
$totaliszhengpinnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and iszhengpin=1");
$totalispianyinum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and ispianyi=1");
$totaliswuliunum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and iswuliu=1");
$totaliszhiliangnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and iszhiliang=1");
$totalisfuwunum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and isfuwu=1");
$totalisqitanum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}' and isqita=1");
	
if($op=='judgment_list'){
	$gid = $_GPC['gid'];
	$list = pdo_fetchall("select id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') as time,iszhuijia,ishaoyong,iszhengpin,ispianyi,iswuliu,iszhiliang,isfuwu,isqita from".tablename('tg_judgment')." where gid ='{$gid}' ORDER BY create_time desc limit 0,10");
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}'  ORDER BY create_time DESC ");
	include wl_template('comment');
}

if($op=='judgment_list_jishi'){
	$gid = $_GPC['gid'];
	$id = $_GPC['gid'];
//评价统计
$totalnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' ");
$totaliszhuijianum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and iszhuijia=1 ");
$totalishaoyongnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and ishaoyong=1");
$totaliszhengpinnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and iszhengpin=1");
$totalispianyinum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and ispianyi=1");
$totaliswuliunum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and iswuliu=1");
$totaliszhiliangnum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and iszhiliang=1");
$totalisfuwunum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and isfuwu=1");
$totalisqitanum = pdo_fetchcolumn('SELECT COUNT(id) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$id}' and isqita=1");

	$list = pdo_fetchall("select id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') as time,iszhuijia,ishaoyong,iszhengpin,ispianyi,iswuliu,iszhiliang,isfuwu,isqita from".tablename('tg_judgment')." where jishi_id ='{$gid}' ORDER BY create_time desc limit 0,10");
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_judgment') . " where jishi_id ='{$gid}'  ORDER BY create_time DESC ");
	include wl_template('comment_jishi');
}

if($op=='judgment_detail'){
	$gid = $_GPC['id'];
	$list = pdo_fetchall("select id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') as time,iszhuijia,ishaoyong,iszhengpin,ispianyi,iswuliu,iszhiliang,isfuwu,isqita from".tablename('tg_judgment')." where gid ='{$gid}' ORDER BY create_time desc limit 0,10");
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_judgment') . " where gid ='{$gid}'  ORDER BY create_time DESC ");
	include wl_template('judgment_detail');
}

if($op=='subPJ'){
	$gid=$_GPC['gid'];
	$txt=$_GPC['txt'];
	$list=pdo_insert('tg_judgment',array('gid'=>$gid,'txt'=>$txt));
	if($list==false){
		die(json_encode(array('status'=>0)));
	}else{
		die(json_encode(array('status'=>1)));
	}
}

//加载评价详情
if($op=='ajax_detail'){
	$gid=$_GPC['id'];
	$list = pdo_fetch("select * from".tablename('tg_collect')."where id ='{$gid}'");
	$goods = pdo_fetch("select * from".tablename('tg_goods')."where id ='{$list['sid']}'");
	$goods['gimg']=tomedia($goods['gimg']);
	$seller = pdo_fetch("select * from".tablename('tg_saler')."where id ='{$list['jishi_id']}'");
	if($list==false){
		die(json_encode(array('status'=>0)));
	}else{
		$list['goods']=$goods;
		$list['seller']=$seller;
		die(json_encode($list));
	}
}

//提交评价内容
if($op=='judgment'){
	$gid=$_GPC['id'];
	$si=str_split($_GPC['si']);
	$content=$_GPC['content'];
	$col=pdo_fetch("select * from ".tablename('tg_collect')." where id=:id",array(':id'=>$gid));
	$ava=pdo_fetch("select * from ".tablename('tg_member')." where openid=:openid and uniacid=:uniacid",array(':openid'=>$_W['openid'],':uniacid'=>$_W['uniacid']));
	$ISzuijia=pdo_fetch("select iszhuijia from ".tablename('tg_judgment')." where openid=:openid and col_id=:col_id",array(':openid'=>$_W['openid'],':col_id'=>$gid));
	if($ISzuijia['iszhuijia']=='0'){
		$iszhuijia='1';
	}else{
		$iszhuijia='0';
	}
	$ishaoyong=intval($_GPC['ishaoyong']);
	$iszhengpin=intval($_GPC['iszhengpin']);
	$ispianyi=intval($_GPC['ispianyi']);
	$iswuliu=intval($_GPC['iswuliu']);
	$iszhiliang=intval($_GPC['iszhiliang']);
	$isfuwu=intval($_GPC['isfuwu']);
	$isqita=intval($_GPC['isqita']);
	if(empty($_GPC['jud_id'])){


		$list=array(
			'col_id'=>$gid,
			'uniacid' => $_W['uniacid'],
			'openid' => $_W['openid'],
			'orderno' => $col['orderno'],
			'gid' => $col['sid'],
			'openname' => $ava['nickname'],
			'jishi_id' => $col['jishi_id'],
			'avatar' => $ava['avatar'],
			'iszhuijia' => $iszhuijia,
			'ishaoyong' => $ishaoyong,
			'iszhengpin' =>$iszhengpin,
			'ispianyi' =>$ispianyi,
			'iswuliu' =>$iswuliu,
			'iszhiliang' =>$iszhiliang,
			'isfuwu' => $isfuwu,
			'isqita' => $isqita,
			'create_time'=>TIMESTAMP
		);
		pdo_insert('tg_judgment',$list);
		$judgment_id=pdo_insertid();
		$isfrist=0;
	}else{
		$judgment_id=$_GPC['jud_id'];
		$isfrist=1;
	}
	pdo_insert('tg_judgment_content',array('judgment_id'=>$judgment_id,'content'=>$content,'isfirst'=>$isfrist,'update_time'=>TIMESTAMP,'who'=>0));
	pdo_update('tg_collect',array('judgment_id'=>$judgment_id),array('id'=>$gid));
	die($judgment_id);
}
//提交评价打分
if($op=='evaluate'){
	$jishiid=$_GPC['jishiid'];
	$gid=$_GPC['gid'];
	$cartid=$_GPC['cartid'];
	$evas=$_GPC['evas'];

	$sum;
	for($i=0;$i<count($evas);$i++){
		$list=array(
			'gid'=>$gid,
			'uniacid' => $_W['uniacid'],
			'jishiid' => $jishiid,
			'cartid' => $cartid,
			'name' => $evas[$i]['name'],
			'weight' => $evas[$i]['weight'],
			'score' => $evas[$i]['score']
		);
		pdo_insert('tg_evaluateDetail',$list);
		$sum+=$evas[$i]['score'];
	}
	if($list==false){
		die(json_encode(array('status'=>0)));
	}else{
		pdo_update('tg_collect',array('score'=>$sum),array('id'=>$cartid));
		die(json_encode(array('status'=>1)));
	}
}
//加载评价详情
if($op=='ajaxJudgmt'){
	$gid=$_GPC['id'];
	$list = pdo_fetch("select * from".tablename('tg_judgment')." where col_id ='{$gid}'");
	die(json_encode($list));
}


if($op =='ajax'){
	$page = $_GPC['page'];
	$pagesize = $_GPC['pagesize'];
	$gid = $_GPC['gid'];	
	$pindex = max(1, intval($page));
	$con=" gid ='{$gid}'";
	if(!empty($_GPC['name'])){
		if($_GPC['name']=='iszhuijia')
		{
			$con.=" and iszhuijia=1";
		}
		if($_GPC['name']=='ishaoyong')
		{
			$con.=" and ishaoyong=1";
		}
		if($_GPC['name']=='iszhengpin')
		{
			$con.=" and iszhengpin=1";
		}
		if($_GPC['name']=='ispianyi')
		{
			$con.=" and ispianyi=1";
		}
		if($_GPC['name']=='iswuliu')
		{
			$con.=" and iswuliu=1";
		}
		if($_GPC['name']=='iszhiliang')
		{
			$con.=" and iszhiliang=1";
		}
		if($_GPC['name']=='isfuwu')
		{
			$con.=" and isfuwu=1";
		}
		if($_GPC['name']=='isqita')
		{
			$con.=" and isqita=1";
		}
	}

	$list = pdo_fetchall("select id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') as time,iszhuijia,ishaoyong,iszhengpin,ispianyi,iswuliu,iszhiliang,isfuwu,isqita from".tablename('tg_judgment')." where {$con} ORDER BY create_time desc LIMIT " . ($pindex - 1) * $pagesize . ',' . $pagesize );
	foreach($list as &$li) {
		$main_content = pdo_fetch("select * from ".tablename('tg_judgment_content')." where  judgment_id=:judgment_id and isfirst=0 order by update_time asc ",array('judgment_id'=>$li['id']));
		$li['main_content'] = $main_content['content'];
		$allcontent = pdo_fetchall("select who,content,FROM_UNIXTIME(update_time,'%Y-%m-%d %H:%i:%s') as time from ".tablename('tg_judgment_content')." where  judgment_id=:judgment_id  order by update_time asc ",array('judgment_id'=>$li['id']));
		$li['contents'] = $allcontent;
		$scoreC=pdo_fetch('select score as scoreC from '.tablename('tg_collect').' where judgment_id=:id',array(':id'=>$li['id']));
		$li['scoreC']=$scoreC['scoreC'];
	}
	die(json_encode($list));
}

if($op =='ajax_jishi'){
	$page = $_GPC['page'];
	$pagesize = $_GPC['pagesize'];
	$gid = $_GPC['gid'];
	$pindex = max(1, intval($page));
	$con=" jishi_id ='{$gid}'";
	if(!empty($_GPC['name'])){
		if($_GPC['name']=='iszhuijia')
		{
			$con.=" and iszhuijia=1";
		}
		if($_GPC['name']=='ishaoyong')
		{
			$con.=" and ishaoyong=1";
		}
		if($_GPC['name']=='iszhengpin')
		{
			$con.=" and iszhengpin=1";
		}
		if($_GPC['name']=='ispianyi')
		{
			$con.=" and ispianyi=1";
		}
		if($_GPC['name']=='iswuliu')
		{
			$con.=" and iswuliu=1";
		}
		if($_GPC['name']=='iszhiliang')
		{
			$con.=" and iszhiliang=1";
		}
		if($_GPC['name']=='isfuwu')
		{
			$con.=" and isfuwu=1";
		}
		if($_GPC['name']=='isqita')
		{
			$con.=" and isqita=1";
		}
	}
	$list = pdo_fetchall("select id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') as time,iszhuijia,ishaoyong,iszhengpin,ispianyi,iswuliu,iszhiliang,isfuwu,isqita from".tablename('tg_judgment')." where {$con} ORDER BY create_time desc LIMIT " . ($pindex - 1) * $pagesize . ',' . $pagesize );
	foreach($list as &$li) {
		$main_content = pdo_fetch("select * from ".tablename('tg_judgment_content')." where  judgment_id=:judgment_id and isfirst=0 order by update_time asc ",array('judgment_id'=>$li['id']));
		$li['main_content'] = $main_content['content'];
		$allcontent = pdo_fetchall("select who,content,FROM_UNIXTIME(update_time,'%Y-%m-%d %H:%i:%s') as time from ".tablename('tg_judgment_content')." where  judgment_id=:judgment_id and isfirst=1  order by update_time asc ",array('judgment_id'=>$li['id']));
		$li['contents'] = $allcontent;
	}
	die(json_encode($list));
}

exit();